package com.koron.plan.web.repairplan.bean.vo;

import com.koron.plan.config.Constants;
import com.koron.plan.web.repairplan.bean.entity.PlanPackageEquipmentBean;
import com.koron.plan.web.repairplan.bean.entity.PlanPackageIssueBean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: PlanPackageIssueVO
 * @projectName GDH_EAM_V2
 * @description: 检修计划包-工单计划VO
 * @date 2021/9/1310:13
 */
@Data
@ApiModel("检修计划包-工单计划VO")
public class PlanPackageIssueVO extends PlanPackageIssueBean {

    @ApiModelProperty("检修计划包-工单计划-设备列表")
    private List<PlanPackageEquipmentBean> equipments;

    @ApiModelProperty("计划内的设备名称，多个之间逗号分隔")
    private String equipmentName;

    @ApiModelProperty("检修计划包-计划状态颜色")
    private String color;

    @ApiModelProperty("计划包编号")
    private String packageCode;

    public String getEquipmentName() {

        if (CollectionUtils.isEmpty(equipments)) {
            return equipmentName;
        }
        List<String> equipmentNames = equipments.stream().map(PlanPackageEquipmentBean::getEquipmentName).collect(Collectors.toList());
        return StringUtils.join(equipmentNames,",");
    }

    public String getColor() {
        if(Constants.REPAIR_PACKAGE_ISSUE_STATE.FINISH.equals(this.getStatus())) {
           // 已完成(如已经完成，则表明更新时间为计划完成时间)
            if((getUpdateTime().compareTo(getEndTime()) <= 0)) {
                // 完成时间 <= 预计结束时间
                return Constants.Signal.ON_TIME;
            } else {
                // 完成时间 > 预计结束时间
                return Constants.Signal.EXPIRED;
            }
        } else {
            // 未完成
            if((getEndTime().getTime() - System.currentTimeMillis()) > Constants.PLAN_EXPIRING_DURATION) {
                // 距离计划结束超过3天时间
                return Constants.Signal.ON_TIME;
            } else  if((getEndTime().getTime() - System.currentTimeMillis()) > 0 && (getEndTime().getTime() - System.currentTimeMillis()) <= Constants.PLAN_EXPIRING_DURATION) {
                // 0  <  预计完成时间 - 当前时间  < 3天   ===> 即还剩不到3天就到计划完成时间了
                return Constants.Signal.EXPIRING;
            } else {
                // 预计计划结束时间已经超过3天了
                return Constants.Signal.EXPIRED;
            }
        }
    }
}
